layui.use(["layer","laydate","table","jquery","form"],function () {
    var
        layer = layui.layer,
        laydate = layui.laydate,
        table = layui.table,
        form = layui.form,
        $ = layui.jquery;
    laydate.render({
        elem:"#createDate",
        type:"datetime"
    })
    $.ajax({
        url:"/JsUserFeedback/feedbackCount",
        data:{},
        type:'post',
        dataType:'json',
        success:function (data) {
            console.log(data);
            $("#stay-examine").text("("+data.data.stayExamine+")");
            $("#already-examine").text("("+data.data.alreadyExamine+")");
            $("#All-examine").text("("+(Number(data.data.alreadyExamine)+Number(data.data.stayExamine))+")");

        }
    })
    //全部审核按钮
    $("#select-All-examine").click(function () {
        var param = {};
        getAllFeedback(param)
        $("#select-All-examine").css("backgroundColor","#009688");
        $("#select-All-examine").css("color","#FFFFFF");
        $("#All-examine").css("color","#FFFFFF");

        $("#select-already-examine").css("backgroundColor","white");
        $("#select-already-examine").css("color","green");
        $("#already-examine").css("color","green");

        $("#select-stay-examine").css("backgroundColor","white");
        $("#select-stay-examine").css("color","green");
        $("#stay-examine").css("color","green");
    });
    //待审核按钮
    $("#select-stay-examine").click(function () {
        var stayExamine = 3;
        var alreadyExamine = "";
        QueryByStatusFeedback(stayExamine,alreadyExamine);
        $("#select-All-examine").css("backgroundColor","white");
        $("#select-All-examine").css("color","green");
        $("#All-examine").css("color","green");

        $("#select-stay-examine").css("backgroundColor","#009688");
        $("#select-stay-examine").css("color","#FFFFFF");
        $("#stay-examine").css("color","#FFFFFF");

        $("#select-already-examine").css("backgroundColor","white");
        $("#select-already-examine").css("color","green");
        $("#already-examine").css("color","green");


    });
    //已审核按钮
    $("#select-already-examine").click(function () {
        var stayExamine = "";
        var alreadyExamine = 3;
        QueryByStatusFeedback(stayExamine,alreadyExamine);
        $("#select-already-examine").css("backgroundColor","#009688");
        $("#select-already-examine").css("color","#FFFFFF");
        $("#already-examine").css("color","#FFFFFF");

        $("#select-All-examine").css("backgroundColor","white");
        $("#select-All-examine").css("color","green");
        $("#All-examine").css("color","green");

        $("#select-stay-examine").css("backgroundColor","white");
        $("#select-stay-examine").css("color","green");
        $("#stay-examine").css("color","green");
    });
    //根据不同状态查询对应数据
    function QueryByStatusFeedback(stayExamine,alreadyExamine){
        $.ajax({
            url:"/JsUserFeedback/QueryByStatusFeedback",
            data:{"stayExamine":stayExamine,"alreadyExamine":alreadyExamine},
            type:'post',
            dataType:"json",
            success:function (data) {
                showFeedback(data.data);
            }
        });
    }
    $("#refresh").click(function () {
        location.reload();
    });
    $('#previous').click(function () {
        parent.tabParentChange(3);
    })
    //初始化查询所有数据
    var layForm = form.val("layForm");
    getAllFeedback(layForm);

    $("#search").click(function () {
        var layForm = form.val("layForm");
        console.log(layForm);
        getAllFeedback(layForm);
    });
    $("#Reset").click(function () {
        $("input[name=account]").val("");
        $("input[name=createDate]").val("");
        $("select[name=feedbackClassification]").val(0);
        $("select[name=feedbackStatus]").val(0);
        form.render();
        var layForm = form.val("layForm");
        getAllFeedback(layForm);
    });
    //获取所有数据
    function getAllFeedback(param){
        $.ajax({
            url:"/JsUserFeedback/getAllJsUserFeedback",
            data:param,
            type:'post',
            dataType:'json',
            success:function (data) {
                console.log(data);
                showFeedback(data.data);
            }
        })
    }
    //展示查询到的数据
    function showFeedback(data){
        table.render({
            elem:"#tableTest",
            // width:1650,
            data:data,
            title:"反馈信息",
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
                ,first: true //不显示首页
                ,last: true //不显示尾页
            },
            cols: [[
                {type:'checkbox'}
                ,{field:'nickname',  title: '用户昵称', sort: true,align:"center"}
                ,{field:'account', title: '用户账号',align:"center"}
                ,{field:'createDate', title: '反馈时间',align:"center"}
                ,{field:'feedbackContent',  title: '反馈内容',align:"center"}
                ,{field:'feedbackType', title: '反馈类型',align:"center",templet:function (data) {
                        if (data.feedbackType == 2){
                            return "课程反馈";
                        }else if(data.feedbackType == 3){
                            return "商品反馈";
                        }else if(data.feedbackType == 4){
                            return "其他反馈";
                        }
                    }}
                ,{field:'feedbackStatus', title: '反馈状态', sort: true,align:"center",templet:function (data) {
                        if (data.feedbackStatus == 2){
                            return "已提交";
                        }else if(data.feedbackStatus == 3){
                            return "待审核";
                        }else if(data.feedbackStatus == 4){
                            return "审核通过";
                        }else if(data.feedbackStatus == 5){
                            return "未提交";
                        }else if(data.feedbackStatus == 6){
                            return "审核驳回";
                        }
                    }}
                ,{field:'feedbackClassification', title: '意见分类', sort: true,align:"center",templet:function (data){
                        if (data.feedbackClassification == 2){
                            return "很好";
                        }else if(data.feedbackClassification == 3){
                            return "好";
                        }else if(data.feedbackClassification == 4){
                            return "一般";
                        }else if(data.feedbackClassification == 5){
                            return "差";
                        }
                    }}
                ,{field:'',  title: '操作',align:"center",templet:function (data) {
                        if (data.feedbackStatus == 2){
                            return "<sapn style='color: green;'>已提交</sapn>"+
                                "<a onclick='deleteFeedback()' style='color: green; margin-left: 10px;'>删除</a>";
                        }else if(data.feedbackStatus == 3){
                            return "<a onclick='stayExamine()' style='color: green;'>待审核</a>" +
                                "<a onclick='deleteFeedback()' style='color: green; margin-left: 10px;'>删除</a>";
                        }else if(data.feedbackStatus == 4){
                            return "<a style='color: green;'>审核通过</a>" +
                                "<a onclick='deleteFeedback()' style='color: green; margin-left: 10px;'>删除</a>";
                        }else if(data.feedbackStatus == 5){
                            return "<sapn style='color: green;'>未提交</sapn>"+
                                "<a onclick='deleteFeedback()' style='color: green; margin-left: 10px;'>删除</a>";
                        }else if(data.feedbackStatus == 6){
                            return "<sapn style='color: green;'>审核驳回</sapn>"+
                                "<a onclick='deleteFeedback()' style='color: green; margin-left: 10px;'>删除</a>";
                        }
                    }}
            ]]
        });
        //待审核
        stayExamine = function stayExamine() {
            table.on('row(tableTest)', function(obj){
                console.log(obj.data);
                layer.open({
                    title:"待审核",
                    type:2,
                    area:["800px","500px"],
                    btn:["通过","取消"],
                    content:"feedback-examine.html",
                    success:function (layero, index) {
                        var findObj = layero.find("iframe").contents();
                        findObj.find("input[name=account]").val(obj.data.account);
                        if (obj.data.feedbackType == 2){
                            findObj.find("input[name=feedbackType]").val("课程反馈");
                        }else if(obj.data.feedbackType == 3){
                            findObj.find("input[name=feedbackType]").val("商品反馈");
                        }else if(obj.data.feedbackType == 4){
                            findObj.find("input[name=feedbackType]").val("其他反馈");
                        }

                        if (obj.data.feedbackClassification == 2){
                            findObj.find("input[name=feedbackClassification]").val("很好");
                        }else if(obj.data.feedbackClassification == 3){
                            findObj.find("input[name=feedbackClassification]").val("好");
                        }else if(obj.data.feedbackClassification == 4){
                            findObj.find("input[name=feedbackClassification]").val("一般");
                        }else if(obj.data.feedbackClassification == 5){
                            findObj.find("input[name=feedbackClassification]").val("差");
                        }
                        findObj.find("input[name=feedbackStatus]").val(obj.data.feedbackStatus);
                        if (obj.data.feedbackStatus == 2){
                            findObj.find("input[name=feedbackStatus]").val("已提交");
                        }else if(obj.data.feedbackStatus == 3){
                            findObj.find("input[name=feedbackStatus]").val("待审核");
                        }else if(obj.data.feedbackStatus == 4){
                            findObj.find("input[name=feedbackStatus]").val("审核通过");
                        }else if(obj.data.feedbackStatus == 5){
                            findObj.find("input[name=feedbackStatus]").val("未提交");
                        }else if(obj.data.feedbackStatus == 6){
                            findObj.find("input[name=feedbackStatus]").val("审核驳回");
                        }
                        findObj.find("textarea[name=feedbackContent]").val(obj.data.feedbackContent);
                    },
                    yes:function (index, layero) {
                        var user = JSON.parse(localStorage.getItem("user"));
                        var param = {
                            "feedbackId":obj.data.feedbackId,
                            "feedbackStatus":4,
                            "updateId":user.data.userId,
                            "updateDate":getTime()
                        };
                        console.log(param);
                        $.ajax({
                            url:"/JsUserFeedback/updateFeedbackStatus",
                            data:param,
                            type:'post',
                            dataType:"json",
                            success:function (data) {
                                console.log(data);
                                if (data.data == true) {
                                    getAllFeedback();
                                    layer.msg("审核成功");
                                }else{
                                    layer.msg("审核失败");
                                }

                            }
                        });
                        layer.close(index);
                    },btn2:function (index, layero) {

                    }
                })

            });

        }
        //删除
        deleteFeedback = function deleteFeedback() {
            table.on('row(tableTest)', function(obj){
                $.ajax({
                    url:"/JsUserFeedback/DeleteFeedbackById",
                    data:{"feedbackId":obj.data.feedbackId},
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        if (data.data == true){
                            layer.msg("删除成功");
                        } else{
                            layer.msg("删除失败");
                        }
                    }
                })
            });
        }

    }
    $("#refresh").click(function () {
        location.reload();
    });
//现在时间
    function getTime(){
        var date=new Date();
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        return dateStr;
    }
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }
})